package com.hz.airline.task;

import com.alibaba.fastjson.JSONObject;
import com.hz.airline.entity.Flight;
import com.hz.airline.entity.FlightInfomation;
import com.hz.airline.entity.FlightPlay;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.hz.airline.util.Constants.Logger;

/**
 * Created by kevin on 2020/2/2 10:50
 */
public class BaseServlet {

    // 响应数据
    public String response(int statusCode, String description, Object response) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("statusCode", statusCode);
        jsonObject.put("description", description);
        if (response != null) {
            jsonObject.put("bodyRequest", response);
        }
        String result = jsonObject.toString();
        Logger.info("response：" + result);
        return result;
    }

    // 响应数据
    public JSONObject responsePlay(Flight flight, FlightPlay play, FlightInfomation infomation) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("craft", flight.getCraft());
        jsonObject.put("status", infomation.getStatus());
        jsonObject.put("city", play.getCity());
        jsonObject.put("endcity", play.getEndcity());
        jsonObject.put("departtime", play.getDeparttime());
        jsonObject.put("arrivaltime", play.getArrivaltime());
        jsonObject.put("departterminal", flight.getDepartterminal());
        jsonObject.put("arrivalterminal", flight.getArrivalterminal());
        jsonObject.put("flightnumberid", flight.getFlightnumberid());
        jsonObject.put("cabinname", flight.getCabinname());
        jsonObject.put("luggage", String.valueOf(flight.getLuggage()));
        jsonObject.put("food", flight.getFood() == 0 ? "有餐" : "无餐");
        return jsonObject;
    }

    // 转换日期
    public String convert2Date(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    // 转换时长
    public double converMiletime(double old, String str) {
        String h = str.substring(0, str.indexOf("h"));
        String m = str.substring(str.indexOf("h") + 1, str.indexOf("m"));
        // 转为double
        double d = Double.parseDouble(h) + Double.parseDouble(m) / 100 + old;
        // 与表内值处理
        String format = String.format("%.2f", d);
        String d1 = format.substring(format.indexOf(".") + 1, format.length());
        if (Integer.parseInt(d1) >= 60) {
            d += 0.4;
        }
        String result = String.format("%.2f", d);
        return Double.parseDouble(result);
    }

    // 会员过期时间
    public Date getExpireDate(int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }
}
